<?php
// 计划任务
namespace Admin\Controller;
use Think\Controller;
use Think\Log;
class CronController extends Controller
{
	 
	 // 创建广告主数据
	public function createEverydayAdData($value='')
	{	
		
		if (C('IS_FALSE_DEL')) {
        	$where['a.is_delete']='0';	 
        	$where['b.is_delete']='0';	 
        }else{
        	$where=[];
        }
        // $yesterday  = mktime(date("H"), date("i"), date("s"), date("m"), date("d")-1, date("Y")); 
		
		$where['a.shelf_status']='1';
		 
		// $where1['a.shelf_status'] = '0';
		// $where1['a.off_shelf_time'] =  ['gt',$yesterday];

		// $map['_complex']=[$where,$where1,'_logic' => 'or'];
		
		$data=M('advertisement a')
		->join('wm_advertiser b on a.advertiser_id=b.id')
		->field('a.id as ad_id,a.ad_name,a.advertiser_id as adv_id,b.company_name as adv_name,a.access_unit_price,a.cooperation_type')
		->where($where)
		->select();
		
		$data=array_map(function($val){
			$yesterday  = mktime(date("H"), date("i"), date("s"), date("m"), date("d")-1, date("Y")); 
			
			$val['add_time']=$yesterday;
			return $val;
		}, $data);


		$res=M('advertiser_data')->addAll($data);

		$msg=$res ? '广告数据生成成功' : '广告数据生成失败';

		echo date("Y-m-d H:i:s").' '.$msg;
		echo '\n';
		Log::write($msg);

		 
	}
	
	public function createEverydayChannelData(){
		$PacMode=M('channel_packet');
		$pacresult=$PacMode->alias('a')->join('wm_advertisement b on a.ad_id=b.id')->join('wm_packet d on d.id=a.packet_id')->join('wm_channel c on c.id=a.channel_id')->field('a.packet_id,a.ad_id,a.channel_id,a.cooperation_type,a.unit_price,a.deduction,a.bound_state,a.bound_time,b.ad_name,c.channel_name,d.packet_num')->where('a.bound_state="1"')->select();
		$model=M('channel_data');
		$pacresult=array_map(function($val){
			$val['add_time']=time()-86400;
			return $val;
		}, $pacresult);
		$pacresult=array_map(function($val){
			$val['input_status']="0";
			return $val;
		}, $pacresult);
		$pacresult=array_map(function($val){
			$val['settlement_status']="0";
			return $val;
		}, $pacresult);
		$pacresult=array_map(function($val){
			$val['is_delete']="0";
			return $val;
		}, $pacresult);
		$result=$model->addAll($pacresult);
		/*["id"]=>
	    string(1) "1"a.*,a.*,a.*,a.*,a.*,a.*,a.*,
	    ["ad_id"]=>
	    string(2) "15"
	    ["packet_id"]=>
	    string(1) "1"
	    ["channel_id"]=>
	    string(1) "1"
	    ["cooperation_type"]=>
	    string(3) "cpa"
	    ["unit_price"]=>
	    string(1) "2"
	    ["deduction"]=>
	    string(1) "2"
	    ["bound_state"]=>
	    string(1) "0"
	    ["bound_time"]=>
	    string(19) "0000-00-00 00:00:00"*/

	    $msg=$res ? '广告数据生成成功' : '广告数据生成失败';

		echo date("Y-m-d H:i:s").' '.$res;
		echo '\n';   
		Log::write($msg);
		
	}

}